package com.wxzz.elearing.user.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.AdminRoleDto;
import com.wxzz.elearing.user.dto.AdminRolePermissionDto;
import com.wxzz.elearing.user.dto.EnterprisePermissionDto;
import com.wxzz.elearing.user.entity.AdminPermission;
import com.wxzz.elearing.user.entity.AdminRole;
import com.wxzz.elearing.user.entity.AdminUser;
import com.wxzz.elearing.user.entity.AdminUserRole;
import com.wxzz.elearing.user.service.IAdminPermissionService;
import com.wxzz.elearing.user.service.IAdminRoleService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/auth")
@Api(tags = "后台权限接口(后台)")
public class AuthController {

    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private IAdminPermissionService adminPermissionService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "添加角色", notes = "添加角色", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveAdminRole", method = RequestMethod.POST)
    public ReturnVO saveAdminRole(@RequestHeader String token, @Valid @RequestBody AdminRole adminRole) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        adminRole.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return adminRoleService.saveAdminRole(adminRole);
    }

    @ApiOperation(value = "修改角色信息", notes = "修改角色信息", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateAdminRole", method = RequestMethod.POST)
    public ReturnVO updateAdminRole(@RequestBody AdminRole adminRole) {
        return adminRoleService.updateAdminRole(adminRole);
    }

    @ApiOperation(value = "给用户分配角色", notes = "给用户分配角色", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateAdminUserRole", method = RequestMethod.POST)
    public ReturnVO updateAdminUserRole(@RequestHeader String token, @RequestBody AdminUserRole adminUserRole) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return adminRoleService.updateAdminUserRole(adminUser.getEnterpriseId(), adminUserRole);
    }

    @ApiOperation(value = "给角色分配权限", notes = "给用户分配角色", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateAdminRolePermission", method = RequestMethod.POST)
    public ReturnVO updateAdminRolePermission(@RequestBody AdminRolePermissionDto adminRolePermissionDto) {
        return adminRoleService.updateAdminRolePermission(adminRolePermissionDto);
    }

    @ApiOperation(value = "回显角色对应权限", notes = "回显角色对应权限", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAdminPermissionForRole/{adminRoleId}", method = RequestMethod.POST)
    public ReturnVO<Set<AdminPermission>> listAdminPermissionForRole(@RequestHeader String token, @PathVariable Long adminRoleId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return adminPermissionService.listAdminPermissionForRole(adminRoleId, jsonObject.getLong("enterpriseId"));
    }

    @ApiOperation(value = "token换取用户权限信息", notes = "token换取用户权限信息", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "getAdminUserPermission", method = RequestMethod.POST)
    public ReturnVO<Set<AdminPermission>> getAdminUserPermission(@RequestHeader String token) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return adminPermissionService.getAdminUserPermission(adminUser.getId(), adminUser.getEnterpriseId());
    }

    @ApiOperation(value = "删除角色", notes = "删除角色", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteAdminRole/{id}", method = RequestMethod.POST)
    public ReturnVO deleteAdminRole(@PathVariable Long id) {
        return adminRoleService.deleteAdminRole(id);
    }

    @ApiOperation(value = "角色列表", notes = "角色列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAdminRole", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<AdminRole>>> listAdminRole(HttpServletResponse response, @RequestHeader String token, @RequestBody AdminRoleDto adminRoleDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return adminRoleService.listAdminRole(response, jsonObject.getLong("enterpriseId"), adminRoleDto);
    }

    @ApiOperation(value = "展示当前权限", notes = "展示当前权限", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAdminPermission", method = RequestMethod.POST)
    public ReturnVO<List<AdminPermission>> listAdminPermission(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return adminPermissionService.listAdminPermission(jsonObject.getLong("enterpriseId"));
    }

    @ApiOperation(value = "总后台获取企业权限", notes = "总后台获取企业权限", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAllAdminPermission", method = RequestMethod.POST)
    public ReturnVO<List<AdminPermission>> listAllAdminPermission() {
        return adminPermissionService.listAllAdminPermission();
    }

    @ApiOperation(value = "给企业分配权限", notes = "给企业分配权限", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveEnterpriseAdminPermission", method = RequestMethod.POST)
    public ReturnVO saveEnterpriseAdminPermission(@Valid @RequestBody EnterprisePermissionDto enterprisePermissionDto) {
        return adminPermissionService.saveEnterpriseAdminPermission(enterprisePermissionDto);
    }

    @ApiOperation(value = "展示企业拥有的权限", notes = "展示企业拥有的权限", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAdminPermissionByEnterpriseId/{enterpriseId}", method = RequestMethod.POST)
    public ReturnVO<List<AdminPermission>> listAdminPermissionByEnterpriseId(@PathVariable Long enterpriseId) {
        return adminPermissionService.listAdminPermission(enterpriseId);
    }
}
